'use strict';

angular
  .module('cmsysApp')
  .service('privilegeService',
    [
      '$http',
      'baseService',
      'pageService',
      function ($http, baseService, pageService) {
        var self = this;
        this.roleList = function ($scope) {
          self.params = $scope.params || {};
          pageService.query($scope.params).then(function (page) {
            pageService.loadData($scope, page);
          });
        };
        this.del = function (del) {
          baseService.del(del)
        };
        this.edit = function (edit) {
          baseService.edit(edit)
        };
      }])
  .controller(
    'privilegeCtrl',
    [
      '$scope',
      '$sce',
      '$http',
      '$state',
      '$stateParams',
      'privilegeService',
      'pageService',
      '$filter',
      'baseService',
      'userPrivileges',
      function ($scope, $sce, $http, $state, $stateParams, privilegeService, pageService, $filter, baseService, userPrivileges) {
        var privileges = "", cutRid;
        $scope.opt = userPrivileges['删除角色'] || userPrivileges['修改角色'] || userPrivileges['设置角色权限'];
        baseService.query({url: baseUrl + '/privilege/privileges'}).then(function (data) {
          $.fn.zTree.init($('#tree'), {
            check: {
              enable: true
            },
            view: {
              dblClickExpand: true,
              showLine: true,
              selectedMulti: false
            },
            data: {
              simpleData: {
                enable: true,
                idKey: "pno",
                pIdKey: "father",
                rootPId: ""
              }
            },
          }, data.privileges);
        });

        $scope.params = {
          'url': baseUrl + '/role/roles',
          'conditions': {
            'condition': '',
            'currentPage': 1,
            'pageSize': 10
          }
        };
        $scope.go = function (pageNum) {
          $scope.params.conditions.currentPage = pageNum;
          privilegeService.roleList($scope);
        };
        $scope.operations = {
          'del': {
            'url': baseUrl + '/role/delRole'
          },
          'edit': {
            'url': baseUrl + '/role/editRole'
          }
        };
        $scope.changePS = function () {
          pageService.changePS($scope);
          privilegeService.roleList($scope);
        };
        $scope.changeCD = function () {
          pageService.changeCD($scope);
          privilegeService.roleList($scope);
        };
        privilegeService.roleList($scope);

        $scope.edit = function (index) {
          $scope.title = '修改角色';
          var role = $scope.page.recordList[index];
          $scope.role = role;
          $('#role').modal('toggle');
        };
        $scope.del = function (index) {
          $scope.operations.del.params = {rid: index};
          privilegeService.del($scope.operations.del);
          privilegeService.roleList($scope);
        };
        $scope.addRole = function () {
          $scope.title = '添加角色';
          $('#role').modal('toggle');
        };

        $scope.save = function (flag) {
          if (flag) {
            $('#privilege').modal('toggle');
            var tree = $.fn.zTree.getZTreeObj('tree'), nodes = tree.getCheckedNodes(true), temp = [];
            for (var i = 0; i < nodes.length; i++) temp.push(nodes[i].pid);
            privileges = temp.join(",");
            baseService.edit({
              url: baseUrl + '/privilege/setPrivilege',
              params: {rid: cutRid, privileges: privileges}
            });
            privileges = "";
            tree.checkAllNodes(false);
          } else {
            $('#role').modal('toggle');
            $scope.operations.edit.params = $scope.role;
            privilegeService.edit($scope.operations.edit);
            $scope.role = {};
            $scope.myForm.$setPristine();
          }
          privilegeService.roleList($scope);
        };

        $scope.cancel = function (flag) {
          if (flag) {
            $('#privilege').modal('toggle');
            $.fn.zTree.getZTreeObj('tree').checkAllNodes(false);
          } else {
            $scope.role = {};
            $scope.myForm.$setPristine();
            $('#role').modal('toggle');
          }
        };

        function getAllNodes(nodes, allNodes) {
          if (nodes) {
            for (var i = 0; i < nodes.length; i++) {
              allNodes.push(nodes[i]);
              getAllNodes(nodes[i].children, allNodes);
            }
          }
        };

        $scope.setPrivilege = function (rid) {
          cutRid = rid;
          var tree = $.fn.zTree.getZTreeObj('tree'), nodes = tree.getNodes(), allNodes = [];
          getAllNodes(nodes, allNodes);
          baseService.query({url: baseUrl + '/privilege/privilege', params: {rid: rid}}).then(function (data) {
            var privilege = data.privilege;
            for (var i = 0; i < privilege.length; i++) {
              for (var j = 0; j < allNodes.length; j++) {
                // console.log(allNodes[i] + '---' + privilege[i].pid);
                if (allNodes[j].pid == privilege[i].pid) {
                  tree.checkNode(allNodes[j], true);
                }
              }
            }
          });
          $('#privilege').modal('toggle');
        }
      }
    ])
;
